package com.yunkeji.api.dataUpload.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yunkeji.common.exception.ServiceException;
import com.yunkeji.common.utils.DataUploadUtils;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.SysUpload;
import com.yunkeji.dao.mapper.SysUploadMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 数据上报回调接口
 *
 * @author 雷擎宇
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dataUpload")
@Slf4j
public class DataUploadCallBackController {

    private final SysUploadMapper mapper;

    @GetMapping("/oppo")
    public void oppo(DataUploadUtils.OPPORequestBody req){
        if(StringUtils.isEmpty(req.getOaid()) || Objects.isNull(req.getAdid())){
            throw new ServiceException("OAID和ADID不能为空！");
        }
        SysUpload upload = mapper.selectOne(Wrappers.lambdaQuery(SysUpload.class).eq(SysUpload::getOaid, req.getOaid()));
        if(!Objects.isNull(upload)) {
            return;
        }
        upload = BeanUtil.toBean(req, SysUpload.class);
        mapper.insert(upload);
    }

//    @GetMapping("/test")
//    public int test(){
//        String oaid = ServletUtils.getRequest().getHeader("oaid");
//        mapper.upload(oaid);
//        return 1;
//    }
}
